<template>
    <div class="city-alphabet">
        <ul class="list">
            <li
                class="item"
                v-for="key in letters"
                :key="key"
                :ref="key"
                @click="sendLetter"
                @touchstart="touchstart"
                @touchmove="touchmove"
                @touchend="touchend"
            >{{ key }}</li>
        </ul>
    </div>
</template>

<script>
export default {
    name: 'CityAlphabet',
    data() {
        return {
            touchStatus: false,
            elStartY: '',
            timer: null
        }
    },
    updated() {
        this.elStartY = this.$refs['A'][0].offsetTop
    },
    props: {
        cities: Object
    },
    computed: {
        letters() {
            const letters = []
            for (let key in this.cities) {
                letters.push(key)
            }
            return letters
        }
    },
    methods: {
        sendLetter(e) {
            this.$emit('change', e.target.innerText)
        },
        touchstart() {
            this.touchStatus = true
        },
        touchmove(e) {
            if (this.touchStatus) {
                if (this.timer) {
                    clearTimeout(this.timer)
                }
                this.timer = setTimeout(() => {
                    const touchY = e.touches[0].clientY - 79
                    const index = Math.floor((touchY - this.elStartY) / 20)
                    if (index >= 0 && index < this.letters.length) {
                        this.$emit('change', this.letters[index])
                    }
                }, 16)
            }
        },
        touchend() {
            this.touchStatus = false
        }
    }
}
</script>

<style lang="scss" scoped>
@import '~styles/varibles.scss';
.city-alphabet{
    .list{
        display: flex;
        flex-direction: column;
        justify-content: center;
        position: absolute;
        right: 0;
        top: 1.58rem;
        bottom: 0;
        width: 0.4rem;
        .item{
            color: $bgColor;
            line-height: 0.4rem;
            text-align: center;
        }
    }
}
</style>
